package com.wenlin.customerservice.controller;

import com.wenlin.customerservice.client.UserClient;
import com.wenlin.customerservice.domain.User;
import com.wenlin.customerservice.response.Message;
import com.wenlin.customerservice.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 *  Created with IntelliJ IDEA.
 *  User:  wenlin
 *  Date:  2017/11/24
 *  Time:  9:17
 *  Description:
 **/
@RestController
@RequestMapping("customer")
@Api(description = "分布式锁测试")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @Autowired
    private UserClient userClient;
    /***
     *  User:  wenlin
     *  Date:  2017/11/24
     *  Time:  9:20
     *  Description:获取用户信息controller
     **/
    @GetMapping
    public Message<User> get(Integer id){
        return customerService.get(id);
    }


    @GetMapping("/lock")
    @ApiOperation(value="10次http请求")
    public Message<String> getLock(){
        System.out.println(userClient.getFairLock());
        for(int i=0;i<10;i++){
            new Thread(userClient::getLock).start();
        }
        return new Message<>("Success");
    }

    @GetMapping("/fairlock")
    @ApiOperation(value="公平锁")
    public Message<Boolean> getFairLock(){
        for(int i=0;i<10;i++){
            new Thread(userClient::getFairLock).start();
        }
        return new Message<>(true);
    }
}
